package com.chhuang.utils.string;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES256加解密
 * @author 黄晨
 *
 */
public class AES256Encryption {
	private AES256Encryption(){}
	
	public static final String KEY_ALGORITHM = "AES";
	public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";

	public static byte[] initkey() throws Exception {
		// 实例化密钥生成器
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM, "BC");
		kg.init(256);
		kg.init(128);
		SecretKey secretKey = kg.generateKey();
		return secretKey.getEncoded();
	}

	public static byte[] initRootKey() throws Exception {
		return new byte[] { 0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c, 0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07,
				0x0a, 0x04, 0x0f, 0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09, 0x06, 0x07, 0x09, 0x0d };
	}

	public static Key toKey(byte[] key) throws Exception {
		SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
		return secretKey;
	}

	public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
		Key k = toKey(key);
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
		cipher.init(Cipher.ENCRYPT_MODE, k);
		return cipher.doFinal(data);
	}

	public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
		Key k = toKey(key);
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
		cipher.init(Cipher.DECRYPT_MODE, k);
		return cipher.doFinal(data);
	}

	public static byte[] decrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
		Key k = toKey(key);
		IvParameterSpec ivSpec = new IvParameterSpec(iv); 
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
		cipher.init(Cipher.DECRYPT_MODE, k, ivSpec);
		return cipher.doFinal(data);
	}
	
	public static byte[] encrypt(byte[] data) throws Exception {
		return encrypt(data, initkey());
	}

	public static byte[] decrypt(byte[] data) throws Exception {
		return decrypt(data, initkey());
	}
	
	public static String encrypt(String str) throws Exception {
		return Base64CodeUtil.encode(encrypt(str.getBytes(), initRootKey()));
	}

	public static String decrypt(String str) throws Exception {
		return new String(decrypt(Base64CodeUtil.decode(str), initRootKey()));
	}

	public static void main(String[] args) throws Exception {
		String str = "asdfqer3453ger%$%$&sdfgwer";
		str = encrypt(str);
		System.out.println("密文："+str);
		str = decrypt(str);
		System.out.println("明文："+str);
	}
}
